package intro.databases.gtCarRental.ui;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.GTCRApplication.GTCR_MemberType;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.ui.employee.Employee_Home;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CreateAccount extends GTCRActivity {
	Spinner userType;
	EditText username, password, cPassword, creditCard;
	LinearLayout creditCardNo;

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.create_account);

		creditCardNo = (LinearLayout) this.findViewById(R.id.linearLayout_createAccount_creditCardNo);		
		userType = (Spinner) findViewById(R.id.spinner_createAccount_typeOfUser);
		
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.gtcr_userTypes, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		userType.setAdapter(adapter);
		userType.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				TextView t = (TextView) arg1;
				if(t.getText().toString().equals("GT Student/Faculty")) {
					creditCardNo.setVisibility(View.VISIBLE);
				} else {
					creditCardNo.setVisibility(View.GONE);
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});

		username = (EditText) findViewById(R.id.editText_createAccount_username);
		password = (EditText) findViewById(R.id.editText_createAccount_password);
		cPassword = (EditText) findViewById(R.id.editText_createAccount_confirmPassword);
		creditCard = (EditText) findViewById(R.id.editText_createAccount_creditCard);
	}

	public void cancel(View v) {
		Intent intent = new Intent(this, Login.class);
		startActivity(intent);
		this.finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Intent intent = new Intent(this, Login.class);
		startActivity(intent);		
	}

	public void register(View v) {
		if(!(username.getText().toString().equals("") || password.getText().toString().equals("") || cPassword.getText().toString().equals(""))) {
			if(password.getText().toString().equals(cPassword.getText().toString())) {
				registerUser();
			} else {
				Toast.makeText(this, "Passwords don't match", Toast.LENGTH_LONG).show();
			}
		} else {
			Toast.makeText(this, "Please fill out all fields", Toast.LENGTH_LONG).show();
		}
	}

	public void registerUser() {
		GTCR_MemberType type = GTCR_MemberType.MEMBER;
		String userTypeString = ((TextView) userType.getSelectedView()).getText().toString();
		if(userTypeString.equals("GTCR Employee")) {
			type = GTCR_MemberType.EMPLOYEE;			
			this.dbConn.createAccount(username.getText().toString(), password.getText().toString(), type, 0);
			application.currentUser = dbConn.getUserFromCredentials(username.getText().toString(), password.getText().toString());
			Intent intent = new Intent(this, Employee_Home.class);
			startActivity(intent);
		} else if (userTypeString.equals("GT Student/Faculty")) {
			type = GTCR_MemberType.MEMBER;
			this.dbConn.createAccount(username.getText().toString(), password.getText().toString(), type, Long.parseLong(creditCard.getText().toString()));
			application.currentUser = dbConn.getUserFromCredentials(username.getText().toString(), password.getText().toString());
			Intent intent = new Intent(this, Home.class);
			startActivity(intent);
			Intent intent2 = new Intent(this, PersonalInformation.class);
			startActivity(intent2);
		} else {
			type = GTCR_MemberType.MEMBER;
		}
		
		this.finish();
	}
}
